package com.jf.cloud.service.sysmanager.handler.WxSmalleDecode;

import org.apache.commons.codec.binary.Base64;

import net.sf.json.JSONObject;


/**
 * 封装对外访问方法
 * @author liuyazhuang
 *
 */
public class WXCore {

    private static final String WATERMARK = "watermark";
    private static final String APPID = "appid";
    /**
     * 解密数据
     * @return
     * @throws Exception
     */
    public static String decrypt(String appId, String encryptedData, String sessionKey, String iv){
        String result = "";
        try {
            AES aes = new AES();
            byte[] resultByte = aes.decrypt(Base64.decodeBase64(encryptedData), Base64.decodeBase64(sessionKey), Base64.decodeBase64(iv));
            if(null != resultByte && resultByte.length > 0){
                result = new String(WxPKCS7Encoder.decode(resultByte));
                JSONObject jsonObject = JSONObject.fromObject(result);
                String decryptAppid = jsonObject.getJSONObject(WATERMARK).getString(APPID);
                if(!appId.equals(decryptAppid)){
                    result = "";
                }
            }
        } catch (Exception e) {
            result = "";
            e.printStackTrace();
        }
        return result;
    }


//    public static void main(String[] args) throws Exception{
//        String appId = "wxe7bd25efd17ae483";
//        String encryptedData = "c3qyGENnqBTifzRXTkSNZtXQwjtqrQJCc1KwNaOw1MvrAC1FBpOc30ZG58HF+6VSZ90jYEBwGYwmeoq2etmTHJLvqkakVqFBzf9lsQZKznqlpnQtK+RbFHOto54zTP7Zf3obp7GxDYjtebvnuxMsQn+ahmfhg0Q6gn/dKfmjaeKEOa6Iz4/A5i1RCGDYTe8kOeqaF6Il4AbgzQxJ0D5iPQ==";
//        String sessionKey = "W73mL5/iK4ezx0p7A7T/OQ==";
//        String iv = "+O9HkKFpp9PpVcT2Lll2iQ==";
//        System.out.println(decrypt(appId, encryptedData, sessionKey, iv));
//    }
}